﻿/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the tools applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qtpropertybrowserutils_p.h"
#include <QtCore/QLocale>
#include <QtGui/QMouseEvent>
#include <QtGui/QPainter>
#include <QtWidgets/QApplication>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QMenu>

QT_BEGIN_NAMESPACE

QtCursorDatabase::QtCursorDatabase()
{
	appendCursor(Qt::ArrowCursor, QCoreApplication::translate("QtCursorDatabase", "Arrow"),
				 QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-arrow.png")));
	appendCursor(Qt::UpArrowCursor, QCoreApplication::translate("QtCursorDatabase", "Up Arrow"),
				 QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-uparrow.png")));
	appendCursor(Qt::CrossCursor, QCoreApplication::translate("QtCursorDatabase", "Cross"),
				 QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-cross.png")));
	appendCursor(Qt::WaitCursor, QCoreApplication::translate("QtCursorDatabase", "Wait"),
				 QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-wait.png")));
	appendCursor(Qt::IBeamCursor, QCoreApplication::translate("QtCursorDatabase", "IBeam"),
				 QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-ibeam.png")));
	appendCursor(Qt::SizeVerCursor, QCoreApplication::translate("QtCursorDatabase", "Size Vertical"),
				 QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-sizev.png")));
	appendCursor(Qt::SizeHorCursor, QCoreApplication::translate("QtCursorDatabase", "Size Horizontal"),
				 QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-sizeh.png")));
	appendCursor(Qt::SizeFDiagCursor, QCoreApplication::translate("QtCursorDatabase", "Size Backslash"),
				 QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-sizef.png")));
	appendCursor(Qt::SizeBDiagCursor, QCoreApplication::translate("QtCursorDatabase", "Size Slash"),
				 QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-sizeb.png")));
	appendCursor(Qt::SizeAllCursor, QCoreApplication::translate("QtCursorDatabase", "Size All"),
				 QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-sizeall.png")));
	appendCursor(Qt::BlankCursor, QCoreApplication::translate("QtCursorDatabase", "Blank"),
				 QIcon());
	appendCursor(Qt::SplitVCursor, QCoreApplication::translate("QtCursorDatabase", "Split Vertical"),
				 QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-vsplit.png")));
	appendCursor(Qt::SplitHCursor, QCoreApplication::translate("QtCursorDatabase", "Split Horizontal"),
				 QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-hsplit.png")));
	appendCursor(Qt::PointingHandCursor, QCoreApplication::translate("QtCursorDatabase", "Pointing Hand"),
				 QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-hand.png")));
	appendCursor(Qt::ForbiddenCursor, QCoreApplication::translate("QtCursorDatabase", "Forbidden"),
				 QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-forbidden.png")));
	appendCursor(Qt::OpenHandCursor, QCoreApplication::translate("QtCursorDatabase", "Open Hand"),
				 QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-openhand.png")));
	appendCursor(Qt::ClosedHandCursor, QCoreApplication::translate("QtCursorDatabase", "Closed Hand"),
				 QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-closedhand.png")));
	appendCursor(Qt::WhatsThisCursor, QCoreApplication::translate("QtCursorDatabase", "What's This"),
				 QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-whatsthis.png")));
	appendCursor(Qt::BusyCursor, QCoreApplication::translate("QtCursorDatabase", "Busy"),
				 QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-busy.png")));
}

void QtCursorDatabase::clear()
{
	m_cursorNames.clear();
	m_cursorIcons.clear();
	m_valueToCursorShape.clear();
	m_cursorShapeToValue.clear();
}

void QtCursorDatabase::appendCursor(Qt::CursorShape shape, const QString& name, const QIcon& icon)
{
	if (m_cursorShapeToValue.contains(shape))
		return;
	const int value = m_cursorNames.count();
	m_cursorNames.append(name);
	m_cursorIcons.insert(value, icon);
	m_valueToCursorShape.insert(value, shape);
	m_cursorShapeToValue.insert(shape, value);
}

QStringList QtCursorDatabase::cursorShapeNames() const
{
	return m_cursorNames;
}

QMap<int, QIcon> QtCursorDatabase::cursorShapeIcons() const
{
	return m_cursorIcons;
}

QString QtCursorDatabase::cursorToShapeName(const QCursor& cursor) const
{
	int val = cursorToValue(cursor);
	if (val >= 0)
		return m_cursorNames.at(val);
	return QString();
}

QIcon QtCursorDatabase::cursorToShapeIcon(const QCursor& cursor) const
{
	int val = cursorToValue(cursor);
	return m_cursorIcons.value(val);
}

int QtCursorDatabase::cursorToValue(const QCursor& cursor) const
{
#ifndef QT_NO_CURSOR
	Qt::CursorShape shape = cursor.shape();
	if (m_cursorShapeToValue.contains(shape))
		return m_cursorShapeToValue[shape];
#endif
	return -1;
}

#ifndef QT_NO_CURSOR
QCursor QtCursorDatabase::valueToCursor(int value) const
{
	if (m_valueToCursorShape.contains(value))
		return QCursor(m_valueToCursorShape[value]);
	return QCursor();
}
#endif

QPixmap QtPropertyBrowserUtils::brushValuePixmap(const QBrush& b)
{
	QImage img(16, 16, QImage::Format_ARGB32_Premultiplied);
	img.fill(0);

	QPainter painter(&img);
	painter.setCompositionMode(QPainter::CompositionMode_Source);
	painter.fillRect(0, 0, img.width(), img.height(), b);
	QColor color = b.color();
	if (color.alpha() != 255)
	{// indicate alpha by an inset
		QBrush opaqueBrush = b;
		color.setAlpha(255);
		opaqueBrush.setColor(color);
		painter.fillRect(img.width() / 4, img.height() / 4,
						 img.width() / 2, img.height() / 2, opaqueBrush);
	}
	painter.end();
	return QPixmap::fromImage(img);
}

QIcon QtPropertyBrowserUtils::brushValueIcon(const QBrush& b)
{
	return QIcon(brushValuePixmap(b));
}

QString QtPropertyBrowserUtils::colorValueText(const QColor& c)
{
	return QCoreApplication::translate("QtPropertyBrowserUtils", "[%1, %2, %3] (%4)")
			.arg(c.red())
			.arg(c.green())
			.arg(c.blue())
			.arg(c.alpha());
}

QPixmap QtPropertyBrowserUtils::fontValuePixmap(const QFont& font)
{
	QFont f = font;
	QImage img(16, 16, QImage::Format_ARGB32_Premultiplied);
	img.fill(0);
	QPainter p(&img);
	p.setRenderHint(QPainter::TextAntialiasing, true);
	p.setRenderHint(QPainter::Antialiasing, true);
	f.setPointSize(13);
	p.setFont(f);
	QTextOption t;
	t.setAlignment(Qt::AlignCenter);
	p.drawText(QRect(0, 0, 16, 16), QString(QLatin1Char('A')), t);
	return QPixmap::fromImage(img);
}

QIcon QtPropertyBrowserUtils::fontValueIcon(const QFont& f)
{
	return QIcon(fontValuePixmap(f));
}

QString QtPropertyBrowserUtils::fontValueText(const QFont& f)
{
	return QCoreApplication::translate("QtPropertyBrowserUtils", "[%1, %2]")
			.arg(f.family())
			.arg(f.pointSize());
}

QString QtPropertyBrowserUtils::dateFormat()
{
	QLocale loc;
	QString format = loc.dateFormat(QLocale::ShortFormat);
	// Change dd.MM.yy, MM/dd/yy to 4 digit years
	if (format.count(QLatin1Char('y')) == 2)
		format.insert(format.indexOf(QLatin1Char('y')), QLatin1String("yy"));
	return format;
}

QString QtPropertyBrowserUtils::timeFormat()
{
	QLocale loc;
	// ShortFormat is missing seconds on UNIX.
	return loc.timeFormat(QLocale::LongFormat);
}

QString QtPropertyBrowserUtils::dateTimeFormat()
{
	QString format = dateFormat();
	format += QLatin1Char(' ');
	format += timeFormat();
	return format;
}

QtBoolEdit::QtBoolEdit(QWidget* parent)
	: QWidget(parent),
	  m_checkBox(new QCheckBox(this)),
	  m_textVisible(true)
{
	QHBoxLayout* lt = new QHBoxLayout;
	if (QApplication::layoutDirection() == Qt::LeftToRight)
		lt->setContentsMargins(4, 0, 0, 0);
	else
		lt->setContentsMargins(0, 0, 4, 0);
	lt->addWidget(m_checkBox);
	setLayout(lt);
	connect(m_checkBox, SIGNAL(toggled(bool)), this, SIGNAL(toggled(bool)));
	setFocusProxy(m_checkBox);
	m_checkBox->setText(tr("True"));
}

void QtBoolEdit::setTextVisible(bool textVisible)
{
	if (m_textVisible == textVisible)
		return;

	m_textVisible = textVisible;
	if (m_textVisible)
		m_checkBox->setText(isChecked() ? tr("True") : tr("False"));
	else
		m_checkBox->setText(QString());
}

Qt::CheckState QtBoolEdit::checkState() const
{
	return m_checkBox->checkState();
}

void QtBoolEdit::setCheckState(Qt::CheckState state)
{
	m_checkBox->setCheckState(state);
}

bool QtBoolEdit::isChecked() const
{
	return m_checkBox->isChecked();
}

void QtBoolEdit::setChecked(bool c)
{
	m_checkBox->setChecked(c);
	if (!m_textVisible)
		return;
	m_checkBox->setText(isChecked() ? tr("True") : tr("False"));
}

bool QtBoolEdit::blockCheckBoxSignals(bool block)
{
	return m_checkBox->blockSignals(block);
}

void QtBoolEdit::mousePressEvent(QMouseEvent* event)
{
	if (event->buttons() == Qt::LeftButton)
	{
		m_checkBox->click();
		event->accept();
	}
	else
	{
		QWidget::mousePressEvent(event);
	}
}

QT_END_NAMESPACE
